package com.ctvit.cch.web.controller;

import com.ctvit.cch.core.pojo.dto.creative.CreativeScriptDesignGenerateDTO;
import com.ctvit.cch.core.pojo.dto.creative.CreativeScriptDesignRegenerateDTO;
import com.ctvit.cch.core.pojo.vo.creative.CreativeScriptDesignGetVO;
import com.ctvit.cch.core.pojo.vo.creative.CreativeScriptSceneListVO;
import com.ctvit.cch.core.service.ICreativeScriptDesignService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;

import jakarta.servlet.http.HttpServletResponse;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 脚本设计Controller
 *
 * @author heyingcheng
 * @date 2025/10/11
 */
@RestController
@RequestMapping("/creative/script/design")
public class CreativeScriptDesignController extends BaseController {

    private final ICreativeScriptDesignService creativeScriptDesignService;

    public CreativeScriptDesignController(ICreativeScriptDesignService creativeScriptDesignService) {
        this.creativeScriptDesignService = creativeScriptDesignService;
    }

    /**
     * 生成脚本设计
     */
    @Log(title = "脚本设计-生成脚本", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('creative:script:design:generate')")
    @PostMapping("/generate")
    public AjaxResult generate(@RequestBody @Validated CreativeScriptDesignGenerateDTO dto) {
        return toAjax(creativeScriptDesignService.generate(dto));
    }

    /**
     * 重新生成脚本设计
     */
    @Log(title = "脚本设计-重新生成脚本", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('creative:script:design:generate')")
    @PostMapping("/regenerate")
    public AjaxResult regenerate(@RequestBody @Validated CreativeScriptDesignRegenerateDTO dto) {
        return toAjax(creativeScriptDesignService.regenerate(dto));
    }

    /**
     * 获取脚本设计详细信息
     *
     * @param id 脚本ID
     * @return {@link AjaxResult} 详细信息
     */
    @PreAuthorize("@ss.hasPermi('creative:script:design:get')")
    @PostMapping(value = "get")
    public AjaxResult get(@RequestParam String id) {
        return success(creativeScriptDesignService.get(id));
    }

    @Log(title = "脚本设计-下载脚本", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('creative:script:design:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestParam String scriptId)
            throws UnsupportedEncodingException {
        CreativeScriptDesignGetVO vo = creativeScriptDesignService.get(scriptId);
        List<CreativeScriptSceneListVO> scenes = vo.getScenes();
        ExcelUtil<CreativeScriptSceneListVO> util = new ExcelUtil<CreativeScriptSceneListVO>(
                CreativeScriptSceneListVO.class);
        util.exportExcel(response, scenes, "脚本设计");
    }

}